package com.fingard.dsp.bank.directbank.bjums01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IGateway;
import com.fingard.dsp.bank.reqrespobj.gateway.ReqGateway;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;
import com.fingard.text.XmlHelper;

import java.util.HashMap;
import java.util.Map;

public class BJUMS01Gateway extends BJUMS01Base implements IGateway {
    @Override
    public HashMap<String, String> GenGwMap(ReqGateway p_reqGateway) throws Exception {
        HashMap<String, String> retMap = new HashMap<String, String>();

        try{
            String ownAct = p_reqGateway.reqHashMap.get("OurBankAccount");
            ActSetItem actItem = this.getBankConfig().getActSetItem(ownAct, "");
            if (actItem != null) {
                Map<String, Object> param = getParam(actItem);
                param.put("tranno",  BJUMS01TransType.QR001002);
                param.put("reqOrderNo",  p_reqGateway.reqHashMap.get("RdSeq"));
                param.put("source",  p_reqGateway.reqHashMap.get("PayType"));
                param.put("amount",  StringHelper.amountMultiply100(p_reqGateway.reqHashMap.get("Amount")));
                param.put("currency",   "CNY");
                String notify_url = actItem.notifyURL;
                if (StringHelper.hasAnyChar(notify_url)) {
                    param.put("notifyUrl", notify_url);//后台通知地
                }
                String clientIp = actItem.createIP;
                if (StringHelper.hasAnyChar(clientIp)) {
                    param.put("clientIp", clientIp);//后台通知地
                }
                param.put("subject",  XmlHelper.encode(p_reqGateway.reqHashMap.get("Description")));
                param.put("description", "");
                param.put("timeExpire", p_reqGateway.reqHashMap.get("TimeExpire"));
                boolean limit = getBankConfig().getValueAsBool("limitPay");
                WriteBankUrlLogLn("能否用信用卡支付："+limit);
                param.put("limitCreditCard", limit);
                param.put("body", XmlHelper.encode(p_reqGateway.reqHashMap.get("Attach")));
                param.put("merchId",  actItem.merchantId);
                param.put("account", p_reqGateway.reqHashMap.get("CellPhone"));
                param.put("authenOrderNo", "");
                String sign = addSign(null, param, actItem.saltKey);
                WriteBankLogLn("签名结果:"+sign);
                param.put("signature", sign);
                JSONObject jsonObj=new JSONObject(param);
                String[] tmpStrRet = sendToBank(jsonObj.toJSONString(),bankFront.serverURL);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    String  pay_result = resultObj.getString("respCode");
                    String resp_msg = resultObj.getString("respMsg");

                    if("000000".equals(pay_result)){
                        retMap.put("RespCode", "0");
                        retMap.put("RespInfo", "签名成功");
                        retMap.put("Result", resultObj.getString("payInfo"));
                    }else {
                        retMap.put("RespCode", "-1");
                        retMap.put("RespInfo", "签名失败:"+resp_msg);
                    }

                }else {
                    retMap.put("RespCode", "-1");
                    retMap.put("RespInfo", "签名失败:"+tmpStrRet[1]);
                }
            } else {
                retMap.put("RespCode", "-1");
                retMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retMap.put("RespCode", "-1");
            retMap.put("RespInfo", ex.getMessage());
            WriteBankLogLn(ex);
        }

        return retMap;
    }

    @Override
    public RetAtsQueryOrder queryGWPay(ReqQueryOrder p_reqGateway) throws Exception {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_reqGateway.rdSeq;
        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqGateway.ownAct, "");
            if (actItem != null) {
                Map<String, Object> param = getParam(actItem);
                param.put("tranno",  BJUMS01TransType.QR002001);
                param.put("reqOrderNo",  p_reqGateway.rdSeq);
                param.put("orderNo",  "");
                param.put("merchId",  actItem.merchantId);
                String sign = addSign(null, param, actItem.saltKey);
                WriteBankLogLn("签名结果:"+sign);
                param.put("signature", sign);
                JSONObject jsonObj=new JSONObject(param);
                String[] tmpStrRet = sendToBank(jsonObj.toJSONString(),bankFront.serverURL);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    String  pay_result = resultObj.getString("respCode");
                    String resp_msg = resultObj.getString("respMsg");
                    String paySt = resultObj.getString("paySt");
                    retAts.respCode = "0";
                    retAts.respInfo = resp_msg;
                    retAts.outTransID  = p_reqGateway.outTransID;
                    if("000007".equals(pay_result)){
                        retAts.transState = pay_result;
                    }else{
                        retAts.transState = paySt;
                    }
                    retAts.payInfoCode = pay_result;
                }else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_reqGateway.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}